package com.mylib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.mylib.libcore.R;
import com.mylib.libcore.utils.UIUtils;

/**
 * @author Administrator
 * @date 2019/12/20-9:38
 * @description CustomImageTitleView-自定义图片标题view
 **/
public class CustomImageTitleView extends LinearLayout {
    private Context mContext;
    private View mView;
    private RelativeLayout relativeLayout;
    private ImageView imageView;
    private ImageView imageViewState;
    private TextView textView;
    private static final int DEFAULT_BORDER_TEXT_COLOR = Color.BLACK;

    private int mTextColor = DEFAULT_BORDER_TEXT_COLOR;
    private String mTextTitle = "";
    private int mImageId = R.color.white;


    public CustomImageTitleView(Context context) {
        this(context, null);
    }

    public CustomImageTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomImageTitleView, defStyleAttr, 0);
        mTextColor = a.getColor(R.styleable.CustomImageTitleView_cus_image_title_text_color, DEFAULT_BORDER_TEXT_COLOR);
        mTextTitle = a.getString(R.styleable.CustomImageTitleView_cus_image_title_text)+"";
        // 读取资源文件类型的属性
        mImageId = a.getResourceId(R.styleable.CustomImageTitleView_cus_image_title_img, 0);

        textView.setText(mTextTitle);
        textView.setTextColor(mTextColor);
        imageView.setImageResource(mImageId);
        a.recycle();
    }




    private void init(Context context) {
        mContext = context;
        Log.d("CustomImageTitleView","CustomImageTitleView");
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.custom_image_title_layout, this, true);
        relativeLayout = (RelativeLayout) mView.findViewById(R.id.rl_image_and_title);
        imageView = (ImageView) mView.findViewById(R.id.iv_item_img);
        imageViewState = (ImageView) mView.findViewById(R.id.iv_item_state);
        textView = (TextView) mView.findViewById(R.id.tv_item_title);
    }

    /**
     * 必须初始化
     */
    public void initCustomImageTitle(int drawable,String title) {
        imageView.setBackgroundResource(drawable);
        textView.setText(title+"");
    }
    public void setTitleText(String title) {
        if(!UIUtils.isEmpty(textView)){
            textView.setText(title+"");
        }
    }

    public void showIvState(){
        if(!UIUtils.isEmpty(imageViewState)){
            imageViewState.setVisibility(VISIBLE);
        }
    }
    public void hideIvState(){
        if(!UIUtils.isEmpty(imageViewState)){
            imageViewState.setVisibility(GONE);
        }
    }


}
